package com.cidp.system.dao.impl;

import java.io.Serializable;
import java.util.List;

import com.cidp.system.dao.IOperatorDao;
import com.cidp.system.model.Toperator;
import com.cidp.system.pojo.OperatorDetailPo;
import com.cidp.system.pojo.OperatorPo;
import com.litt.core.common.Utility;
import com.litt.core.dao.BaseDaoImpl;
import com.litt.core.exception.BusiException;
import com.litt.core.page.IPageList;
import com.litt.core.page.PageParam;
import com.litt.core.page.PagePoParam;

/**
 * <b>标题：</b>操作员管理DAO实现类.
 * <pre><b>描述</b>
 *    操作员管理
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @since 2008-4-10
 * @version 1.0
 */
public class OperatorDaoImpl extends BaseDaoImpl implements IOperatorDao 
{
	/**
	 * 根据登陆ID查找
	 * @param loginId 登陆ID
	 * @return Toperator对象
	 */
	public Toperator loadByLoginId(String loginId)
	{
		return (Toperator)super.loadObject(Toperator.class, "loginId", loginId);
	}
	
	/**
	 * 逻辑删除操作员.
	 * 
	 * @param opId 操作员ID
	 */
	public void deleteLogic(Long opId)
	{		
		Toperator operator = (Toperator)super.loadObject(Toperator.class, opId);
		operator.setStatus(new Long(2));
		super.updateObject(operator);
	}

	/**
	 * 恢复逻辑删除的操作员.
	 * 
	 * @param opId 操作员ID
	 */
	public void resumeLogic(Long opId)
	{		
		Toperator operator = (Toperator)super.loadObject(Toperator.class, opId);
		operator.setStatus(new Long(0));
		super.updateObject(operator);
	}
	
	/**
	 * 注销操作员
	 * 
	 * @param opId
	 */
	public void logoutLogic(Long opId){
		Toperator operator = (Toperator)super.loadObject(Toperator.class, opId);
		operator.setStatus(new Long(1));
		super.updateObject(operator);
	}
	
	/**
	 * 不可改变操作员
	 * 
	 * @param opId
	 */
	public void nochangeLogic(Long opId){
		Toperator operator = (Toperator)super.loadObject(Toperator.class, opId);
		operator.setStatus(new Long(9));
		super.updateObject(operator);
	}
	/**
	 * 分页查询用户
	 * @param pageParam 分页参数对象
	 * 
	 * @return IPageList对象
	 */
	public IPageList listByPage(PagePoParam pagePoParam)
	{
		OperatorPo obj = (OperatorPo) pagePoParam.getConds();
		StringBuffer listHql = new StringBuffer(" from Toperator where 1=1");
		StringBuffer listCountHql = new StringBuffer("select count(*) from Toperator where 1=1");
		if(obj.getLoginId()!=null && !obj.getLoginId().trim().equals("")){
			listHql.append(" and loginId='"+obj.getLoginId().trim()+"'");
			listCountHql.append(" and loginId='"+obj.getLoginId().trim()+"'");
		}
		if(obj.getOpName()!=null && !obj.getOpName().trim().equals("")){
			listHql.append(" and opName like '%"+obj.getOpName().trim()+"%'");
			listCountHql.append(" and opName like '%"+obj.getOpName().trim()+"%'");
		}
		return super.listHqlPage(listHql.toString(), listCountHql.toString(), pagePoParam.getPageIndex(), pagePoParam.getPageSize());
	}
	/**
     * 根据主键查找对象.
     * @param className 类
     * @param id 主键值
     * @return Object 对象
     */
	public Object loadObject(Class className,Serializable id){
		return super.loadObject(className, id);
	}
	/**
     * 根据对象保存.
     * @param obj 对象
     * @return Long 主键值
     */
	public Long saveObject(Object obj){
		return super.saveObject(obj);
	}
	/**
     * 根据对象更新
     * @param obj 对象
     */
	public void updateObject(Object obj){
		super.updateObject(obj);
	}
	/**
     * 根据对象删除
     * @param obj 对象
     */
	public void deleteObject(Object obj){
		super.deleteObject(obj);
	}
	/**
	 * 是否存在loginId
	 * @param loginId
	 * @return
	 * @throws BusiException
	 */
	public boolean doIsLongId(String loginId,String loginPassword){
		boolean flag=false;
		int i=super.listHqlCount("select count(*) from Toperator where loginId='"+loginId+"' and password='"+Utility.MD5EncodeString(loginPassword)+"'" );
		if(i==0){
			flag=false;
		}else{
			flag=true;
		}
		return flag;
	}
	
	public boolean doIsLongById(String loginId){
		boolean flag=false;
		int i=super.listHqlCount("select count(*) from Toperator where loginId='"+loginId+"'" );
		if(i==0){
			flag=false;
		}else{
			flag=true;
		}
		return flag;
	}
	public List loadRight(String opName){
		return super.listAll("select permissionCode from TrolePermission where roleId in (select roleId from TopRole where opId=(select opId from Toperator where loginId='"+opName+"'))");
	}
}
